package com.academico.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.academico.database.Connect;
import com.academico.model.Matricula;

public class MatriculaDAO {
	
	private Connection connection;

	public MatriculaDAO() {
		this.connection = Connect.getInstance();;
	}
	
	
	public void update(Matricula matricula){
		String query = "UPDATE matricula SET situacaomatricula = '"+ matricula.getSituacaoMatricula() +
			"' where codigocurso = " + matricula.getCodigoCurso() +" AND raaluno = " + matricula.getRaAluno();
		System.out.println(query);
			Statement stmt = null; 
		try {
			stmt = connection.createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
		
	}
	
	public Matricula find(Integer raAluno, Integer codigoCurso){
		String query = "SELECT * FROM matricula WHERE codigocurso = " + codigoCurso + " AND raaluno = " + raAluno;
		System.out.println(query);
		Statement stmt = null; 
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			
			if(rs.next()) {
				Matricula matricula = new Matricula();
				matricula.setCodigoCurso(rs.getInt("codigocurso"));
				matricula.setRaAluno(rs.getInt("raaluno"));
				matricula.setDataMatricula(rs.getDate("datamatricula"));
				matricula.setSituacaoMatricula(Matricula.SituacaoMatricula.valueOf(rs.getString("situacaomatricula")));
				return matricula;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public List<Matricula> findAll(){
		String query = "SELECT * FROM matricula";
		
		System.out.println(query);
		Statement stmt = null; 
		List<Matricula> listaMatricula = new ArrayList<Matricula>();
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			
			while (rs.next()) {
				Matricula matricula = new Matricula();
				matricula.setRaAluno(rs.getInt("raaluno"));
				matricula.setCodigoCurso(rs.getInt("codigocurso"));
				matricula.setDataMatricula(rs.getDate("datamatricula"));
				matricula.setSituacaoMatricula(Matricula.SituacaoMatricula.valueOf(rs.getString("situacaomatricula")));
				listaMatricula.add(matricula);
			}
			return listaMatricula;
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public void save(Matricula matricula){
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy/MM/dd");
		String query = "INSERT INTO matricula (raaluno, codigocurso, datamatricula, situacaomatricula)";
		query += "VALUES ('"+ matricula.getRaAluno() + 
			"','" + matricula.getCodigoCurso() + 
			"','" + sfd.format(matricula.getDataMatricula()) + 
			"','" + matricula.getSituacaoMatricula() + "')";
		
		System.out.println(query);
		
			Statement stmt = null; 
		try {
			stmt = connection.createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
		
	}
	

}
